import 'package:flutter/material.dart';

void main() => runApp( MyApp());

/// 生命周期的处理方法  30
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> with WidgetsBindingObserver{
  late AppLifecycleState _appLifecycleState;
  @override
  void initState() {

    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _appLifecycleState = state;
      print('My App State: $_appLifecycleState');
    });
  }


  @override
  Widget build(BuildContext context){
    return Scaffold(
        body: OrientationBuilder(
          builder: (context,orientation){
            return Center(
              child: Text('hello',style: TextStyle(fontSize: 22.0,
                  color: orientation ==Orientation.portrait?Colors.blue:
                      Colors.red
              ),),
            );
          },
        )
    );
  }
}
